package com.czxy.dem01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Test30 {

    public static void main(String[] args) throws IOException {

        Scanner sc=new Scanner(System.in);
        System.out.println("1.账号登陆");
        System.out.println("2.游客浏览");
        int i = sc.nextInt();
        if (i==1){
            boolean login = login();
            if (login!=true){
                System.out.println("登陆失败");
            }
        }
        if (i==2){
            otherLogin();
            System.out.println("ok");
        }


    }
    public static void otherLogin() throws IOException {
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date=new Date();
        StringBuilder sb=new StringBuilder();
        sb.append("游客").append(df.format(date)).append("进行游览").append("\r\n");
        FileUtils.writeStringToFile(new File("data02.txt"),sb.toString(),true);
    }
    public static boolean login() throws IOException {
        List<String> list = FileUtils.readLines(new File("user.properties"));
        Map<String, String> map = new HashMap<>();
        for (String s : list) {
            String[] split = s.split("=");
            map.put(split[0], split[1]);
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("输入用户名");
        String s = sc.nextLine();
        System.out.println("输入密码");
        String s1 = sc.nextLine();

        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();
        DateFormat d = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        int f = -1;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (s.equals(entry.getKey()) && s1.equals(entry.getValue())) {
                f = 0;
            }
        }
        if (f == -1) {
            Date date = new Date();
            sb1.append(s).append(" ").append(d.format(date)).append("登陆失败").append("\r\n");
            FileUtils.writeStringToFile(new File("data02.txt"), sb1.toString(), true);
            return false;
        } else {
            Date date = new Date();
            sb1.append(s).append(" ").append(d.format(date)).append("登陆成功").append("\r\n");
            FileUtils.writeStringToFile(new File("data02.txt"), sb1.toString(), true);
            return true;
        }
    }
}
